package net.georel.representation.categorisation.datasource;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

public class CreateShapefile {

	public static void writetoShape(FeatureCollection<SimpleFeatureType, SimpleFeature> sfc, CoordinateReferenceSystem crs) throws SchemaException, IOException{
		File newFile = getNewShapeFile();

		    
		
		FeatureSource<SimpleFeatureType, SimpleFeature> outputFS = DataUtilities.source(sfc);
		
		
		
	    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

	    Map<String, Serializable> params = new HashMap<String, Serializable>();
	    params.put("url", newFile.toURI().toURL());
	    params.put("create spatial index", Boolean.TRUE);

	    ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
	    newDataStore.forceSchemaCRS(crs);
	    final SimpleFeatureType TYPE = outputFS.getSchema();
	    newDataStore.createSchema(TYPE);

	    /*
	     * You can comment out this line if you are using the createFeatureType
	     * method (at end of class file) rather than DataUtilities.createType
	     */
	    
	    
	    Transaction transaction = new DefaultTransaction("create");

	    String typeName = newDataStore.getTypeNames()[0];
	    FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = newDataStore.getFeatureSource(typeName);
	    
	    if (featureSource instanceof FeatureStore) {
	    	FeatureStore featureStore = (FeatureStore) featureSource;

	        featureStore.setTransaction(transaction);
	        try {
	            featureStore.addFeatures(sfc);
	            transaction.commit();

	        } catch (Exception problem) {
	            problem.printStackTrace();
	            transaction.rollback();

	        } finally {
	            transaction.close();
	        }
	        //System.exit(0); // success!
	    } else {
	        System.out.println(typeName + " does not support read/write access");
	        //System.exit(1);
	    }
	}

	private static File getNewShapeFile() {
	    String path = "C:/";
	    String newPath = "Output" + ".shp";

	    JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
	    chooser.setDialogTitle("Save shapefile");
	    chooser.setSelectedFile(new File(newPath));

	    int returnVal = chooser.showSaveDialog(null);

	    if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
	        // the user cancelled the dialog
	        System.exit(0);
	    }

	    File newFile = chooser.getSelectedFile();
	    

	    return newFile;
	}
}
